package com.lc.zpyh.sqlite;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.lc.zpyh.bean.Browseentity;

import java.util.ArrayList;
import java.util.List;

public class BrowseDao {
    BrowseSQLiteOpenHelper recordHelper;

    SQLiteDatabase recordsDb;

    public BrowseDao(Context context) {
        recordHelper = new BrowseSQLiteOpenHelper(context);
    }
    //添加搜索记录
    public void add(String commodityname, String shopname, String shopimg, String date,String shopid,String commodityid) {
        Log.e("##", "onItemClick: "+ commodityname+shopname);
        String sql="insert into browseentity (commodityname,shopname,shopimg,date,shopid,commodityid) values (?,?,?,?,?,?)";
        String bindArgs[] = new String[] { commodityname, shopname,shopimg, date,shopid,commodityid};
        recordsDb = recordHelper.getWritableDatabase();
        recordsDb.execSQL(sql,
                bindArgs);
        recordsDb.close();
    }


    //判断是否含有该搜索记录
    public boolean isHasRecord(String commodityid) {
        boolean isHasRecord = false;
        recordsDb = recordHelper.getReadableDatabase();
        String sql="select * from browseentity where commodityid=?";
        Cursor cursor = recordsDb.rawQuery(sql,
                new String[] { commodityid });

        while (cursor.moveToNext()) {
            if (commodityid.equals(cursor.getString(cursor.getColumnIndexOrThrow("commodityid")))) {
                isHasRecord = true;
            }
        }
        //关闭数据库
        recordsDb.close();
        cursor.close();
        return isHasRecord;
    }
    public List<Browseentity> findAll() {
        List<Browseentity> persons = new ArrayList<Browseentity>();
        recordsDb = recordHelper.getReadableDatabase();
        String sql="select * from browseentity";
        Cursor cursor = recordsDb.rawQuery(sql, null);
        while(cursor.moveToNext()){
            Browseentity   person = new Browseentity();
//            person.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
            person.setCommodityid(cursor.getString(cursor.getColumnIndex("commodityid")));
            person.setCommodityname(cursor.getString(cursor.getColumnIndex("commodityname")));
            person.setDate(cursor.getString(cursor.getColumnIndex("date")));
            person.setShopid(cursor.getString(cursor.getColumnIndex("shopid")));

            person.setShopimg(cursor.getString(cursor.getColumnIndex("shopimg")));
            person.setShopname(cursor.getString(cursor.getColumnIndex("shopname")));
//            int id = cursor.getInt(cursor.getColumnIndex("id"));
//            String name = cursor.getString(cursor.getColumnIndex("name"));
//            String code = cursor.getString(cursor.getColumnIndex("code"));
//            StockEntity person = new StockEntity(id, name, code);
            persons.add(person);
        }
        cursor.close();
        recordsDb.close();

        return persons;
    }
    //获取全部搜索记录
    public List<String> getRecordsList() {
        List<String> recordsList = new ArrayList<>();
        recordsDb = recordHelper.getReadableDatabase();
        Cursor cursor = recordsDb.query("records", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
            recordsList.add(name);
        }
        //关闭数据库
        recordsDb.close();
        cursor.close();
        return recordsList;
    }

    //模糊查询
    public List<String> querySimlarRecord(String record,String code){
        String queryStr = "select * from records where name like '%" + record + "%' order by name ";
        List<String> similarRecords = new ArrayList<>();
        Cursor cursor= recordHelper.getReadableDatabase().rawQuery(queryStr,null);

        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
            similarRecords.add(name);
        }

        cursor.close();
        return similarRecords;
    }

    //清空搜索记录
    public void deleteAllRecords() {
        String sql="delete from browseentity";
        recordsDb = recordHelper.getWritableDatabase();
        recordsDb.execSQL(sql);

        recordsDb.close();
    }

}
